<?php

namespace tencentcloud;

use helper\Http;

class Stream
{

  private $streamUrl = 'http://fcgi.video.qcloud.com/common_access';

  private $appId;
  private $pushKey;
  private $time;

  public function __construct($appId=null,$key=null)
  {
    $config = config('cloud.tencent');
    $this->appId = !empty($appId) ? (int)$appId : (int)$config['appid'];
    $this->pushKey = !empty($key) ? $key : $config['live']['push_key'];
    $this->time = time();
  }

  /**
   * 开始云端混流
   * @param $outStreamId
   * @param $inputStreamList
   * @param int $templateId
   * @return string
   */
  public function startMixStream($outStreamId,$inputStreamList,$templateId=0){
    $info = explode('_',$outStreamId);
    $params = [
        'timestamp'=>$this->time,
        'eventId'=>$this->time,
        'interface'=>[
            'interfaceName'=>'Mix_StreamV2',
            'para'=>[
                'app_id'=>$this->appId,
                'interface'=>'mix_streamv2.start_mix_stream_advanced',
                'mix_stream_session_id'=>'session_'.$info[1],
                'output_stream_id'=>$outStreamId,
                'output_stream_type'=>0, //混流名称不能为input_stream_list中的一条
                'input_stream_list'=>$inputStreamList
            ]
        ]
    ];
    if($templateId>0) $params['interface']['para']['mix_stream_template_id'] = $templateId;
    $jsonStr = json_encode($params);
    $options['headers'] = [
        "Content-Type: application/json",
        "Content-Length: ".strlen($jsonStr)
    ];
    $result = Http::post($this->buildUrl('Mix_StreamV2'),$jsonStr,$options);
    return json_decode($result,true);
  }

  /**
   * 结束混流
   * @param $outStreamId
   * @return mixed
   */
  public function cancelMixStream($outStreamId){
    $info = explode('_',$outStreamId);
    $params = [
        'timestamp'=>$this->time,
        'eventId'=>$this->time,
        'interface'=>[
            'interfaceName'=>'Mix_StreamV2',
            'para'=>[
                'app_id'=>$this->appId,
                'interface'=>'mix_streamv2.cancel_mix_stream',
                'mix_stream_session_id'=>'session_'.$info[1],
                'output_stream_id'=>$outStreamId,
            ]
        ]
    ];
    $result = Http::post($this->buildUrl('Mix_StreamV2'),json_encode($params,JSON_UNESCAPED_UNICODE));
    return json_decode($result,true);
  }

  /**
   * 开始录制任务
   * @param $streamId
   * @param int $mixStream //是否混流
   * @return mixed
   */
  public function startRecord($streamId,$mixStream=1){
    $startItme = date('Y-m-d H:i:s',$this->time);
    $endTime = date('Y-m-d H:i:s',$this->time+7200);
    $params = [
        "Param"=>[
            "s"=>[
                "channel_id"=>$streamId,
                "start_time"=>urlencode($startItme),
                "end_time"=>urlencode($endTime),
                "file_format"=>"mp4",
                "mix_stream"=>$mixStream //是否开启输出流类型为1的混流录制
            ],
            "n"=>[
                "task_sub_type"=>1
            ]
        ]
    ];
    dump('请求地址');
    dump($this->buildUrl('Live_Tape_Start'));
    dump('请求参数');
    dump(json_encode($params));
    $result = Http::post($this->buildUrl('Live_Tape_Start'),json_encode($params,JSON_UNESCAPED_UNICODE));
    dump('结果');
    dump($result);
    exit;
    return json_decode($result,true);
  }

  /**
   * 结束录制任务
   * @param $streamId
   * @param $taskId
   * @return mixed
   */
  public function cancelRecord($streamId,$taskId){
    $params = [
        "Param"=>[
            "s"=>[
                "channel_id"=>$streamId
            ],
            "n"=>[
                "task_id"=>$taskId,
                "task_sub_type"=>0
            ]
        ]
    ];
    $result = Http::post($this->buildUrl('Live_Tape_Stop'),json_encode($params,JSON_UNESCAPED_UNICODE));
    return json_decode($result,true);
  }

  /**
   * 拼接请求地址
   * @param $fun
   * @return string
   */
  private function buildUrl($fun){
    $urlParams = [
        'appid'=>$this->appId,
        'interface'=>$fun,
        't'=>$this->time,
        'sign'=>$this->getSign(),
    ];
    $url = $this->streamUrl.'?'.http_build_query($urlParams);
    return $url;
  }

  /**
   * 获取签名
   * @return string
   */
  private function getSign(){
    return md5($this->pushKey.$this->time);
  }

  
}